package com.miao.hong.fang.admin.service;

import com.miao.hong.fang.IConstants;
import com.miao.hong.fang.admin.BackupFileTO;
import com.miao.hong.fang.admin.ContactInfoTO;
import com.miao.hong.fang.course.CourseTO;
import com.miao.hong.fang.course.dao.CourseDAO;
import com.miao.hong.fang.teacher.TeacherTO;
import com.miao.hong.fang.teacher.dao.TeacherDAO;
import com.miao.hong.fang.user.dao.UserDAO;
import com.miao.hong.fang.util.SecurityUtil;
import com.miao.hong.fang.util.StringUtil;
import com.miao.hong.fang.util.Utils;
import com.miao.hong.fang.util.pagination.ListAndCount;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

import org.apache.struts.upload.FormFile;

public class AdminServiceImpl implements AdminService{

	private CourseDAO courseDAO;
	private TeacherDAO teacherDAO;
	private UserDAO userDAO;
		
	public void setTeacherDAO(TeacherDAO teacherDAO) {
		this.teacherDAO = teacherDAO;
	}

	public void setCourseDAO(CourseDAO courseDAO) {
		this.courseDAO = courseDAO;
	}
		
	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	public void addCourse(CourseTO courseTO) {
				
		if(courseTO.getIsHotCourse() == null) {
			courseTO.setIsHotCourse("N");
		}
		if(courseTO.getIsPublicCourse() == null) {
			courseTO.setIsPublicCourse("N");
		}
		if(courseTO.getIsRecommendCourse() == null) {
			courseTO.setIsRecommendCourse("N");
		}
		courseDAO.addCourse(courseTO);
	}

	public ListAndCount searchCourse(String courseTitle, String coursePeriod,
			String courseStartTimeAsStr, String courseEndTimeAsStr,
			String coursePrice, String courseCity, String courseCategory,
			String courseTeacher, String isHotCourse, String isRecommendCourse,
			String isPublicCourse) {

		Timestamp courseStartTime = Utils.stringToTimestamp(courseStartTimeAsStr, IConstants.COURSE_START_TIME_FORMAT);
		Timestamp courseEndTime = Utils.stringToTimestamp(courseEndTimeAsStr, IConstants.COURSE_START_TIME_FORMAT);
		
		
		return courseDAO.searchCourse(courseTitle, StringUtil.isNotEmpty(coursePeriod) ? Integer.valueOf(coursePeriod): null, courseStartTime, courseEndTime, 
				StringUtil.isNotEmpty(coursePrice) ? Integer.valueOf(coursePrice): null, courseCity, courseCategory, 
				courseTeacher, isHotCourse, isRecommendCourse, isPublicCourse);
	}
	
	public CourseTO getCourseDetail(String courseId) {
		return courseDAO.getCourseDetail(courseId);
	}
	
	
	public void updateCourse(CourseTO courseTO) {
		if(courseTO.getIsHotCourse() == null) {
			courseTO.setIsHotCourse("N");
		}
		if(courseTO.getIsPublicCourse() == null) {
			courseTO.setIsPublicCourse("N");
		}
		if(courseTO.getIsRecommendCourse() == null) {
			courseTO.setIsRecommendCourse("N");
		}
		courseDAO.updateCourse(courseTO);
	}
	
	public void deleteCourse(String courseId, String version) {
		courseDAO.deleteCourse(courseId, version);
	}
	
	public List getTeacherByCategory(String category) {
		return teacherDAO.getTeacherByCategory(category);
	}
	
	public boolean validateTeacherName(String name) {
		return teacherDAO.validateTeacherName(name);
	}
	
	public String addTeacher(TeacherTO teacherTO) {
		return teacherDAO.addTeacher(teacherTO);
	}

	public ListAndCount searchTeacher(String teacherName, String teacherDesc, String teachMajor) {
		
		return teacherDAO.searchTeacher(teacherName, teacherDesc, teachMajor);
	}
	
	public void deleteTeacher(String teacherId, String version) {
		teacherDAO.deleteTeacher(teacherId, version);
	}
	
	public TeacherTO getTeacherDetail(String teacherId) {
		
		return teacherDAO.getTeacherDetail(teacherId);
	}
	
	public ListAndCount getCoursesByTeacher(String teacherId) {
		
		return teacherDAO.getCoursesByTeacher(teacherId);
	}
	
	public void updateTeacher(TeacherTO teacherTO) {
		teacherDAO.updateTeacher(teacherTO);
	}
	
	public void addCourseCategory(String courseCategory) {
		courseDAO.addCourseCategory(courseCategory);
	}
	
	public void updateCourseCategory(String oldValue, String newValue) {
		
		int i = courseDAO.updateCourseCategory(oldValue, newValue);
		System.out.println(i + " course has changed the category");
		int j = teacherDAO.updateTeacherMajor(oldValue, newValue);
		System.out.println(j + " teacher has changed the major");
	}
	
	public void deleteCourseCategory(String categoryName) {
		courseDAO.deleteCourseCategory(categoryName);
	}
	
	public List getAllCourse() {
		return courseDAO.getAllCourse();
	}
	
	public void saveTeacherImageToDB(String id, FormFile file) throws Exception {
		BackupFileTO to = new BackupFileTO();
		to.setId(id);
		//to.setFile(file.getFileData());
		to.setFile(SecurityUtil.encryptImage(file.getFileData()));
		
		to.setFileName(file.getFileName());
		to.setFileType("IMAGE");
		to.setCreatedBy("System");
		to.setCreatedDt(new Timestamp(System.currentTimeMillis()));
		to.setUpdatedBy("System");
		to.setUpdatedDt(new Timestamp(System.currentTimeMillis()));
		
		teacherDAO.saveTeacherImageToDB(to);
	}
	
	public BackupFileTO getBackupFile(String id) {
		return teacherDAO.getBackUpFile(id);
	}
	
	public void deleteBackupFile(String id) {
		teacherDAO.deleteBackupFile(id);
	}
	
	public void addContactInfo(String aboutUs, String contantPerson, 
			String phone, String email, String qq, String msn) {
		userDAO.addContactInfo(aboutUs, contantPerson, phone, email, qq, msn);
	}
	
	public ContactInfoTO getContactInfo() {
		return userDAO.getContactInfo();
	}
	public void updateContactInfo(ContactInfoTO to) {
		userDAO.updateContactInfo(to);
	}
	
	public List getAllImages() {
		return teacherDAO.getAllImages();
		
	}
	public byte[] getImage(String id) {
		return teacherDAO.getImage(id);
	}
}
